<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

	public function index()
	{
		$this->load->model('model_kajian');
		$this->load->model('model_kota');
		$this->load->model('model_message');
			
		$data['kota'] = $this->model_kota->get_list_city();
		$data['activity'] = $this->model_message->get_activity();
		$data['kajian'] = $this->model_kajian->sort_kajian(0,4);
		$view['content'] = $this->load->view('home',$data,TRUE);
		$this->load->view('index',$view);
	}
	
	public function about()
	{
		$data['content'] = $this->load->view('about','',TRUE);
		$this->load->view('index',$data);
	}
	
	public function kajian($kota='surabaya',$permalink='')
	{
		$this->load->model('model_kajian');
		$this->load->model('model_kota');
		
		//echo "<pre>".var_dump($_POST)."</pre>";
		$q = array(
			'tema' => '',
			'pemateri' => '',
			'hari' => '',
			'tanggal' => ''
		);
		
		if(isset($_POST['q'])){
			$q = array(
				'tema' => $_POST['q'],
				'pemateri' => $_POST['pemateri'] == ''?'':$_POST['pemateri_id'],
				'hari' => $_POST['hari'],
				'tanggal' => $_POST['tanggal'] == ''?'':date('Y-m-d',strtotime($_POST['tanggal']))
			);
		}
		$data['kota'] = $this->model_kota->get_list_city();
		$data['nama_kota'] = $kota;
		if($permalink != ''){
			$data['kajian_add'] = $this->model_kajian->sort_kajian(0,5);
			$data['kajian'] = $this->model_kajian->get_by_link($permalink);
			$view['content'] = $this->load->view('kajian_detail',$data,TRUE);
		}else{
			$data['kajian'] = $this->model_kajian->sort_kajian(0,30,$kota,$q);
			$view['content'] = $this->load->view('kajian',$data,TRUE);
		}
		$this->load->view('index',$view);
	}
	
	public function daftar()
	{
		if(isset($_POST['data'])){
		
			$this->load->model('model_user');
			
			$check = $this->model_user->check($_POST['data']);
			if($check === true){
			
				$this->load->helper('string');
				$random = random_string('alnum', 16);
				
				$data = $_POST['data'];
				$data['password'] = md5(sha1($data['password']));
				$data['confirm'] = $random;
				$data['last_notif'] = date("Y-m-d H:i:s");
				$data['notif_agenda'] = 'yes';
				$data['notif_update'] = 'yes';
				$data['group_id'] = 3;
				$data['profile_image'] = 'default-128.png';
				$data['is_active'] = 'no';
				
				$this->model_user->add($data);
				
				$html = $this->load->view('email_confirm','',TRUE);
				$html = str_replace("{nama}",$data['nama'],$html);
				$html = str_replace("{url}",base_url()."home/confirm/$random",$html);
				
				$this->load->library('email');	
				
				//config
				$config['wordwrap'] = FALSE;
				$config['mailtype'] = 'html';
				
				$this->email->initialize($config);
				//send to email
				$this->email->from("no-reply@infodakwah.net", "InfoDakwah dot NET");
				$this->email->to($data['email']);

				$this->email->subject("Konfirmasi Pendaftaran");
				$this->email->message($html);

				$this->email->send();
				
				echo '{"status":"success","msg":"Terima Kasih. Cek email untuk mengaktifkan akun Anda"}';
				
			}else{
				echo '{"status":"error","msg":"'.$check.'"}';
			}
			
			return;
		}else{
			$this->load->view('signup');
		}
	}
	
	function confirm($token){
		$this->load->model('model_user');
		
		$respon = $this->model_user->confirm($token);
		
		if($respon->num_rows > 0){
			$id=0;
			$nama="";
			foreach($respon->result_array() as $row){
				$id = $row['id'];
				$nama = $row['nama'];
			}
			
			$data['is_active'] = 'yes';
			$data['confirm'] = '1';
			
			$this->model_user->edit($id,$data);
			
			$this->load->view('success_confirm',array("nama"=>$nama));
		}
	}
	
	public function login()
	{
		if(isset($_POST['email'])){
			$this->load->model('model_user');
			
			$respon = $this->model_user->login($_POST['email'],$_POST['password']);
			
			if($respon){
				echo '{"status":"success","url":"member.html"}';
			}else{
				echo '{"status":"error","msg":"Email dan atau password Anda salah."}';
			}
			
			return;
		}else{
			$this->load->view('login');
		}
	}
	
	public function logout(){
		$this->session->sess_destroy();
		redirect(base_url());
	}
	
	public function lupa_password(){
		if(isset($_POST['email'])){
			
			$this->load->model('model_user');
			
			$respon = $this->model_user->forgotpass($_POST['email']);
			
			if($respon->num_rows > 0){
				$id=0;
				$nama="";
				foreach($respon->result_array() as $row){
					$id = $row['id'];
					$nama = $row['nama'];
					$email = $row['email'];
				}
					
			    $new_pass = md5(sha1($newpass = 'ifd'.rand(10,1000)));
				
				$data = array();
				$data['password'] = $new_pass;
				$this->model_user->edit($id,$data);					 
					 
				$html = $this->load->view('email_forgot_pass','',TRUE);
				$html = str_replace("{nama}",$nama,$html);
				$html = str_replace("{pass}",$new_pass,$html);
				$html = str_replace("{email}",$email,$html);
				$html = str_replace("{url}",base_url()."home/login.html",$html);
				
				$this->load->library('email');	
				
				//config
				$config['wordwrap'] = FALSE;
				$config['mailtype'] = 'html';
				
				$this->email->initialize($config);
				//send to email
				$this->email->from("no-reply@infodakwah.net", "InfoDakwah dot NET");
				$this->email->to($data['email']);

				$this->email->subject("Kirim Ulang Password");
				$this->email->message($html);

				$this->email->send();  
				
				echo '{"status":"success","msg":"Terima Kasih. Cek email untuk mengaktifkan melihat password baru Anda"}';
			}else{
				echo '{"status":"error","msg":"Email Anda Tidak terdaftar."}';
			}
			
		}else{
			$this->load->view('forgot_pass');
		}
		
	}
	
	function ac_pemateri(){
		$this->load->model('model_pemateri');
		
		$q = $_GET['q'];
		
		$pemateri = $this->model_pemateri->get_all(0,10,$q,'yes');

		$output = '';
		foreach($pemateri['data']->result_array() as $row){
			$output .= sprintf("%s|%s\n", $row['nama'].", $row[keterangan], $row[nama_kota]",$row['id']);
		}
		$output = substr($output, 0, -1);
		echo $output;
	}
}
